﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web;

namespace DingTalkAgent.Web.Common.Filter
{
    /// <summary>
    /// 权限拦截
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class AuthorizeFilterAttribute : ActionFilterAttribute
    {
        // private DriverHome.IDAL.ISysStructure ISysStructure;
        public AuthorizeFilterAttribute()
        {
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }

            object[] attrs1 = filterContext.ActionDescriptor.GetCustomAttributes(typeof(myCommonAttribute), true);
            if (attrs1.Length > 0)//公用部分，不做权限判断
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                object[] attrs = filterContext.ActionDescriptor.GetCustomAttributes(typeof(ViewPageAttribute), true);
                var isViewPage = attrs.Length == 1;//当前Action请求是否为具体的导航功能页

                if (this.AuthorizeCore(filterContext, isViewPage) == false)//根据验证判断进行处理
                {
                    filterContext.Result = new ContentResult { Content = @"抱歉,你不具有当前操作的权限！" };//功能权限弹出提示框
                }
                else
                {
                    base.OnActionExecuting(filterContext);
                }
            }
        }
        /// <summary>
        /// 权限判断业务逻辑
        /// </summary>
        /// <param name="filterContext"></param>
        /// <param name="isViewPage">是否为连接页</param>
        /// <returns></returns>
        protected virtual bool AuthorizeCore(ActionExecutingContext filterContext, bool isViewPage)
        {
            var controllerName = filterContext.RouteData.Values["controller"].ToString();
            var RoleID = (int)filterContext.HttpContext.Session["RoleID"];
            var actionName = filterContext.RouteData.Values["action"].ToString();

            if (isViewPage && (controllerName.ToLower() != "main" && actionName.ToLower() != "masterpage"))//如果当前Action请求为具体的功能页并且不是MasterPage页
            {

                var dicName = Models.RoleToken.queryPageAction(RoleID, controllerName, actionName);
                if (dicName.Count() > 0)
                {
                    foreach (var dic in dicName)
                    {
                        //filterContext.RouteData.Values.Add(dic.Key, dic.Value);
                        filterContext.Controller.ViewData[dic.ToLower()] = 1;
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return Models.RoleToken.queryAction(RoleID, controllerName, actionName);
            }
        }
    }
}